package com.nk.system.service.impl;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.nk.system.beans.UserProfileBean;
import com.nk.system.beans.UsersBean;
import com.nk.system.common.search.Search;
import com.nk.system.dao.UserDao;
import com.nk.system.domain.User;
import com.nk.system.service.AuthenticationService;

@Service("authenticationService")
public class AuthenticationServiceImpl extends GenericServiceImpl<User, Integer, UserDao>  implements AuthenticationService{

	private Logger log = Logger.getLogger(AuthenticationServiceImpl.class);
	
	@Autowired
	public AuthenticationServiceImpl(UserDao userDao) {
		super(userDao);
	}

	@Override
	public UsersBean authentication(String username, String password) {
		// TODO Auto-generated method stub
		UsersBean usersBean = null;
		try {
			Search search = new Search(User.class);
			search.addFilterEqual("username", username);
			search.addFilterEqual("password", password);
			User user = dao.searchUnique(search);
			
			if(user != null) {
				usersBean = new UsersBean();
				usersBean.setUsId(user.getId());
				usersBean.setUsFname(user.getFirstname());
				usersBean.setUsLname(user.getLastname());
				usersBean.setUsUsername(user.getUsername());
				usersBean.setUsPassword(user.getPassword());
				usersBean.setUsSchool(user.getSchool());
				usersBean.setUsLevel(user.getLevel());
				usersBean.setUsGrade(user.getGrade());
				
				DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
				Calendar cal =  Calendar.getInstance();
				
				String timeStamp = df.format(df.format(cal.getTime()));
				Date lastLogin = df.parse(timeStamp);
				
				/*String date = year+"-"+month+"-"+day;
				birthday = df.parse(date);
				DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
				   //get current date time with Date()
				   Date date = new Date();
				   System.out.println(dateFormat.format(date));
			 
				   //get current date time with Calendar()
				   Calendar cal = Calendar.getInstance();
				   System.out.println(dateFormat.format(cal.getTime()));
				*/
				
				
				User userUpdate = new User();
				userUpdate.setLastLogin(lastLogin);
				dao.update(userUpdate);
				
			}
			
			/*User user2 = new User();
			Calendar rightNow = Calendar.getInstance(); 
			user2.setLastLogin(rightNow);*/
			
		}catch(Exception e) {
			log.error(e,e);
		}
		return usersBean;
	}
	
	@Override
	public boolean authenticationNewUser(String username, String email) {
		// TODO Auto-generated method stub

		try {
			Search search = new Search(User.class);
			search.addFilterEqual("username", username);
			User user = dao.searchUnique(search);
			
			Search search1 = new Search(User.class);
			search1.addFilterEqual("email", email);
			User user1 = dao.searchUnique(search1);
			
			if(user == null && user1 == null){
				return true;
			}
			
		}catch(Exception e) {
			log.error(e,e);
		}
		return false;
	}

	@Override
	public void insert(String username, String password, String firstname, String lName, String sex, String day, String month, String year, String mail, String school,int level,float grade) {
		// TODO Auto-generated method stub
		
		/*Date birthday = new Date(year+"/"+month+"/"+day);*/
		DateFormat df = new SimpleDateFormat("yyyy-mm-dd");
		Date birthday = null;
		try {
			String date = year+"-"+month+"-"+day;
			birthday = df.parse(date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		User user = new User();
		
		user.setUsername(username);
		user.setPassword(password);
		user.setFirstname(firstname);
		user.setLastname(lName);
		if(sex.equals(1)){
			user.setSex("male");
		}
		else{
			user.setSex("female");
		}
		user.setBirthday(birthday);
		user.setEmail(mail);
		user.setSchool(school);
		user.setLevel(level);
		user.setGrade(grade);
		
		dao.save(user);
		
		/*dao.update(userScore);*/
	}

	@Override
	public UserProfileBean getUserProfile(int id) {
		// TODO Auto-generated method stub
		UserProfileBean userProfileBean = null;
		try {
			Search search = new Search(User.class);
			search.addFilterEqual("id", id);
			User user = dao.searchUnique(search);
			
			if(user != null) {
				userProfileBean = new UserProfileBean();
				userProfileBean.setId(id);
				userProfileBean.setUsername(user.getUsername());
				userProfileBean.setPassword(user.getPassword());
				userProfileBean.setFirstname(user.getFirstname());
				userProfileBean.setLastname(user.getLastname());
				userProfileBean.setSex(user.getSex());
				userProfileBean.setBirthday(user.getBirthday());
				userProfileBean.setSchool(user.getSchool());
				userProfileBean.setLevel(user.getLevel());
				userProfileBean.setGrade(user.getGrade());
				userProfileBean.setEmail(user.getEmail());
			}
			
		}catch(Exception e) {
			log.error(e,e);
		}
		return userProfileBean;
	}

	@Override
	public void setUpdateUserProfile(int id, String fName, String lName, String sex,
			String school, int level, float grade) {
		// TODO Auto-generated method stub
		
		Search search = new Search(User.class);
		search.addFilterEqual("id", id);
		User user = dao.searchUnique(search);
		
		/*User userUpdate = new User();*/
		
		user.setFirstname(fName);
		user.setLastname(lName);
		if(sex.equals(1)){
			user.setSex("male");
		}
		else{
			user.setSex("female");
		}
		user.setSchool(school);
		user.setLevel(level);
		user.setGrade(grade);
		dao.update(user);
	}

	@Override
	public UsersBean authentication2(int id) {
		// TODO Auto-generated method stub
		UsersBean usersBean = null;
		try {
			Search search = new Search(User.class);
			search.addFilterEqual("id", id);
			User user = dao.searchUnique(search);
			
			if(user != null) {
				usersBean = new UsersBean();
				usersBean.setUsId(user.getId());
				usersBean.setUsFname(user.getFirstname());
				usersBean.setUsLname(user.getLastname());
				usersBean.setUsUsername(user.getUsername());
				usersBean.setUsPassword(user.getPassword());
				usersBean.setUsSchool(user.getSchool());
				usersBean.setUsLevel(user.getLevel());
				usersBean.setUsGrade(user.getGrade());
			}
		}catch(Exception e) {
			log.error(e,e);
		}
		return usersBean;
	}
	
}
